package com.sctu.EMSApp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.adapter.TravelNotesListAdapter;
import com.sctu.bean.TravelNotesListBean;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;

import java.util.List;

/**
 * Created by 鑫爷您慢点 on 2015/12/24.
 */
public class TravelNotesListActivity extends Activity {
    private ListView listView;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_travel_notes_list);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();
    }

    /**
     * 跳转当前
     * @param context
     */
    public static void actionStart(Context context){
        Intent intent = new Intent(context, TravelNotesListActivity.class);
        context.startActivity(intent);
    }
    private void initView() {
        listView = (ListView) findViewById(R.id.activity_travel_notes_list_list_view);
        imageView = (ImageView) findViewById(R.id.activity_travel_notes_list_return_img);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        GetVolleyHttp.getInstance(this).getStringGETJsonData(UrlConfig.URL_TRAVEL_NOTES_LIST,
                listener, errorlistener);

    }

    Response.Listener<String> listener = new Response.Listener<String>() {
        @Override
        public void onResponse(String json) {
            List<TravelNotesListBean.Results> list = JSON.parseObject(json, TravelNotesListBean.class).getResults();
            listView.setAdapter(new TravelNotesListAdapter(TravelNotesListActivity.this,list));
        }
    };


    // 当网络请求返回状态不成功（也就不是200）
    Response.ErrorListener errorlistener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            Toast.makeText(TravelNotesListActivity.this, "网络出了些小问题", Toast.LENGTH_LONG)
                    .show();
        }
    };


}
